﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Semantics;
namespace AutoResponse.Dialogue
{
    class Memory : Graph
    {
        public void IntiateGraph()
        {

            string[] nodes = { "类型", "字符", "数字", "[人]", "姓名", "性别", "年龄", "实体", "学校", "城市" };
            string[,] edges =
            {
                {"[人]","姓名","字符"},
                {"[人]","年龄","数字"},
                {"[人]","性别","性别"},
                {"[人]","学校","学校"},
                {"学校","类型","实体"},
                {"学校","城市","城市"}
            };

            for (int i = 0; i < nodes.Length; i++)
            {
                Node node = new Node(nodes[i]);
                Node[] Class = NLP.Dictionary.GetWord(nodes[i]);
                if (Class != null)
                {
                    if (Class.Length == 1)
                        node.Class = Class[0];
                    else if (Class.Length > 1)
                    {
                        string text = "Ambiguous node '" + nodes[i] + "'!\r\n";
                        for (int j = 0; j < Class.Length; j++)
                            text += Class[j].ToString("H");
                        throw new Exception(text);
                    }
                }
                this.EnrollNode(node);
            }
            this.IsKindOfNode = this.ResolveNode("类型");
            this.IsKindOfNode.Id = 2;

            for (int i = 0; i < edges.GetLength(0); i++)
            {
                Edge edge = Edge.ConnectNode(this.ResolveNode(edges[i, 0]), this.ResolveNode(edges[i, 1]), this.ResolveNode(edges[i, 2]));
                this.EnrollEdge(edge);
            }


        }

        public new void Merge(Graph srcgraph)
        {
            for (int i = 0; i < srcgraph.Nodes.Count; i++)
            {
                Node node = this.ResolveNode(srcgraph.Nodes[i].Value);
                if (node == null)
                {
                    node = new Node(srcgraph.Nodes[i].Value);
                    node.Class = srcgraph.Nodes[i].Class;
                    this.EnrollNode(node);
                }
            }

            IEnumerator enumerator  = srcgraph.edgesHash.GetEnumerator();
            while (enumerator.MoveNext())
            {
                Edge edge = (Edge)((DictionaryEntry)enumerator.Current).Value;
                Node outgoing = this.ResolveNode(edge.Outgoing.Value);
                Node incoming = this.ResolveNode(edge.Incoming.Value);
                Node edgetype = this.ResolveNode(edge.Class.Value);
                if (edgetype == null)
                {
                    edgetype = new Node(edge.Class.Value);
                    edgetype.Class = edge.Class.Class;
                    this.EnrollNode(edgetype);
                }

                Edge exist = outgoing.GetEdge(edgetype, Edge.EdgeDirection.Out, incoming);
                if (exist == null)
                {
                    Edge newedge = Edge.ConnectNode(outgoing, edgetype, incoming);
                    this.EnrollEdge(newedge);
                }

                //if (this.ResolveEdge(outgoing, edgetype.Value, incoming) == null)
                //{
                //    Edge newedge = Edge.ConnectNode(outgoing, edgetype, incoming);
                //    this.EnrollEdge(newedge);
                //}
            }
        }
    }
}
